﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Models;

namespace Data_Object
{
    public class DichVuDao: IDichVuDao
    {
        public List<DichVu> All()
        {
            string sql = "SELECT * FROM DichVu";
            return Db.ReadList(sql, Make, null);
        }

        /// <summary>
        /// Creates a Category object based on DataReader.
        /// </summary>
        private static Func<IDataReader, DichVu> Make = reader =>
           new DichVu
           {
               MaDichVu = int.Parse(reader["MaDichVu"].ToString()),
               TenDichVu = reader["TenDichVu"].ToString()
           };


        public DichVu Find(int id)
        {
            string sql = "SELECT * FROM DichVu WHERE MaDichVu = @MaDichVu";
            object[] parms = { "@MaDichVu", id };
            return Db.Read(sql, Make, parms);

        }

        public void Insert(DichVu entity)
        {
            string sql =
           @"INSERT INTO DichVu (TenDichVu) 			
              VALUES (@TenDichVu)";
            entity.MaDichVu = Db.Insert(sql, Take(entity));
        }

        private object[] Take(DichVu entity)
        {
            // Note: Parameters in MS Access are positional. This means that the order of parameters 
            // should match the order in which parameters appear in the sql.
            return new object[]  
            {
                "@TenDichVu", entity.TenDichVu,
                "@MaDichVu", entity.MaDichVu
             
            };
        }

        public void Delete(int id)
        {
            string sql =
            @"DELETE FROM DichVu
               WHERE MaDichVu = @MaDichVu";
            Db.Update(sql, new object[] { "@MaDichVu", id });
        }

        public void Update(DichVu entity)
        {
            string sql =
            @"UPDATE DichVu
                 SET TenDichVu = @TenDichVu                                     
               WHERE MaDichVu = @MaDichVu";

            Db.Update(sql, Take(entity));
        }
    }
}
